angular.module('imsApp').controller('advertisersCreateController', ['$scope', '$rootScope', '$state', 'AdvertisersManager','$stateParams',
    function ($scope, $rootScope, $state, AdvertisersManager,$stateParams) {
    var advertiserId=$state.params.advertiserId;

        $scope.initData = {
        };

        var getPlanDataSuccess = function(result) {
            if(result.c == 's') {
                $scope.formData = result.userAdvertiserVO;
                $scope.title = result.title;
                $scope.oldshortName = $scope.formData.shortName;
            } else {
                alert(result.i);
            }
        };

        //通过HASH判断是否进入新建
        if(!advertiserId) {
            //HASH中没有广告计划ID，进入新建功能，初始化formData
            $scope.planEdit = true;
        } else {
            AdvertisersManager.advertisersDetail(advertiserId).then(function (result) {
                getPlanDataSuccess(result.data);
            })
        }


        var submitSuccess = function(result) {
            if(result.c=="s"){
                $state.go('advertisers',{},{reload:true})
            }else{
                alert(i);
            }
        };

        //提交
        $scope.submit = function() {
            if($scope.checkUserName ()&&$scope.checkShortName()&&$scope.checkDeputy()&&$scope.checkCompanyName()
                &&$scope.checkCompanyPhone()&&$scope.checkContactName()&& $scope.checkContactEmail()&& $scope.checkContactPhone()){
                var url = "";
                if(advertiserId) {
                    AdvertisersManager.advertisersEdit(advertiserId,$scope.formData).then(function (result) {
                        submitSuccess(result.data);
                    })
                } else {
                    AdvertisersManager.advertisersAdd($scope.formData).then(function (result) {
                        submitSuccess(result.data);

                    })
                }

            }

        };

        $scope.edit = function() {
            $scope.planEdit = true;
        };

        $scope.cancle = function() {
            if(!advertiserId) {
                $state.go("advertisers",{reload:true});
            } else {
                if($scope.planEdit) {
                    $state.reload();
                } else {
                    $state.go("advertisers",{reload:true});
                }
            }
        };

        /* //帐号正确
         var userNameSuccess = function(result){
         if(result.c == s){
         //alert("s")
         //正确
         }else{
         alert(result.i);//重名
         }
         };
         var userNameError = function(result){
         alert(result);
         };
         //检查广告主帐号是否重名
         $scope.checkUserName = function(){
         ajaxService.AjaxPost(
         $scope.formData,
         "ims/getChannelName.do",
         userNameSuccess,
         userNameError
         );
         };*/
        //校验数据状态：s，r，e
        $scope.checkData = {
            "userName": "s",
            "deputy":"s",
            "shortName":"s",
            "companyName":"s",
            "companyPhone":"s",
            "contactName":"s",
            "contactEmail":"s",
            "contactPhone":"s"

        };
        $scope.formData = {};
        //提示语信息
        $scope.hintData = {
            "userName":"",
            "deputy":"",
            "shortName":"",
            "companyName":"",
            "companyPhone":"",
            "contactName":"",
            "contactEmail":"",
            "contactPhone":""
        };
        //帐号正确
        var userNameSuccess = function(result){
            if(result.c == "s"){
                $scope.checkData.userName = "r";
            }else{
                $scope.checkData.userName = "e";
                $scope.hintData.userName ="该帐号已存在";
            }
        };
        var userNameError = function(result){
            alert(result);
        };
        //检查广告主帐号是否重名
        $scope.checkUserName = function(){
            if(!advertiserId){
                var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则
                if(!$scope.formData.userName){
                    $scope.checkData.userName = "e";
                    $scope.hintData.userName ="不能为空";
                    return false;
                }else{
                    if(reg.test($scope.formData.userName)){
                        AdvertisersManager.advertisersCheckUsername({userName:$scope.formData.userName}).then(function (result) {
                            console.log(result);
                            userNameSuccess(result.data)
                        })

                        if( $scope.checkData.userName == "r"){
                            return true;
                        }else if( $scope.checkData.userName = "e"){
                            return true;
                        }
                    }else{
                        $scope.checkData.userName = "e";
                        $scope.hintData.userName ="邮箱格式不正确";
                        return false;
                    };
                };
            }else{
                return true;
            }
        };
        //检查渠道负责人
        $scope.checkDeputy = function(){
            var reg = (/[\u4e00-\u9fa5]/g);//汉字正则
            if(!$scope.formData.deputy){
                $scope.checkData.deputy = "e";
                $scope.hintData.deputy ="不能为空";
                return false;
            }else{
                if(reg.test($scope.formData.deputy)){
                    $scope.checkData.deputy = "r";
                    return true;
                }else{
                    $scope.checkData.deputy = "e";
                    $scope.hintData.deputy ="只能输入汉字";
                    return false;
                };
            };
        };
        //检查公司简称
        var shortNameSuccess = function(result){
            if(result.c == "s"){
                $scope.checkData.shortName = "r";
            }else{
                $scope.checkData.shortName = "e";
                $scope.hintData.shortName ="简称已存在";
                return true;
            }

        };
        $scope.checkShortName = function(){
            if($scope.oldshortName == $scope.formData.shortName){
                $scope.checkData.shortName = "r";
                return true;
            }else{
                var reg = /^[\u4E00-\u9FA5]{2,8}$/;
                if(!$scope.formData.shortName){
                    $scope.checkData.shortName = "e";
                    $scope.hintData.shortName ="不能为空";
                    return false;
                }else{
                    if(reg.test($scope.formData.shortName)){
                        AdvertisersManager.advertisersCheckShortName($scope.formData.shortName).then(function (result) {
                            shortNameSuccess(result.data);
                        })
                        if( $scope.checkData.shortName == "r"){
                            return true;
                        }else if( $scope.checkData.shortName = "e"){
                            return false;
                        }
                    }else{
                        $scope.checkData.shortName = "e";
                        $scope.hintData.shortName ="不是汉字或者超出2~8位字符";
                        return false;
                    };
                };
            }
        };
        //检查公司全程
        $scope.checkCompanyName = function(){
            var reg = /^[\u4E00-\u9FA5\w\d]+$/;//只能包括汉字、数字、英文字母正则
            if(!$scope.formData.companyName){
                $scope.checkData.companyName = "e";
                $scope.hintData.companyName ="不能为空";
                return false;
            }else{
                if(reg.test($scope.formData.companyName)){
                    $scope.checkData.companyName = "r";
                    return true;
                }else{
                    $scope.checkData.companyName = "e";
                    $scope.hintData.companyName ="只能输入汉字、数字、英文字母";
                    return false;
                };
            };
        };
        //检查公司电话
        $scope.checkCompanyPhone = function(){
            var reg=/^((0[0-9]{2,3})-)([0-9]{7,8})(-([0-9]{3,}))?$/;//座机号正则
            if(!$scope.formData.companyPhone){
                $scope.checkData.companyPhone = "1";
                return true;
            }else{
                if(reg.test($scope.formData.companyPhone)){
                    $scope.checkData.companyPhone = "r";
                    $scope.hintData.companyPhone ="";
                    return true;
                }else{
                    $scope.checkData.companyPhone = "e";
                    $scope.hintData.companyPhone ="电话格式错误";
                    return false;
                };
            };
        };
        //检查联系人姓名
        $scope.checkContactName = function(){
            var reg =  /^[\u4E00-\u9FA5]{2,5}$/;//仅支持2到5个汉字
            if(!$scope.formData.contactName){
                $scope.checkData.contactName = "e";
                $scope.hintData.contactName ="不能为空";
                return false;
            }else{
                if(reg.test($scope.formData.contactName)){
                    $scope.checkData.contactName = "r";
                    return true;
                }else{
                    $scope.checkData.contactName = "e";
                    $scope.hintData.contactName ="只能是2-5个汉字";
                    return false;
                };
            };
        };
        //检查广告主联系人邮箱
        $scope.checkContactEmail = function(){
            if($scope.formData.contactEmail){
                var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则;
                if(reg.test($scope.formData.contactEmail)){
                    $scope.checkData.contactEmail = "r";
                    return true;
                }else{
                    $scope.checkData.contactEmail = "e";
                    $scope.hintData.contactEmail ="格式不正确";
                    return false;
                };
            }else{
                $scope.checkData.contactEmail = "e";
                $scope.hintData.contactEmail ="不能为空";
                return false;
            }
        };

        //检查联系人手机
        $scope.checkContactPhone = function(){
            var reg = /^1(3|4|5|6|7|8)\d{9}$/;//手机号正则
            if(!$scope.formData.contactPhone){
                $scope.checkData.contactPhone = "e";
                $scope.hintData.contactPhone ="不能为空";
                return false;
            }else{
                if(reg.test($scope.formData.contactPhone)){
                    $scope.checkData.contactPhone = "r";
                    return true;
                }else{
                    $scope.checkData.contactPhone = "e";
                    $scope.hintData.contactPhone ="手机格式错误";
                    return false;
                };
            };
        };


    }])


